package com.tensquare.article.service;

import com.tensquare.article.dao.ChannelDao;
import com.tensquare.article.pojo.Article;
import com.tensquare.article.pojo.Channel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import util.IdWorker;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Alfred
 * @description TODO
 * @date 2021/4/26 19:08
 */
@Service
public class ChannelService {
    @Autowired
    private ChannelDao channelDao;
    @Autowired
    private IdWorker idWorker;

    /**
     * fetch data by rule id
     *
     * @description  增加频道
     * @param
     * @return
     */
    public void add(Channel channel){
        channel.setId(idWorker.nextId()+"");
        channelDao.save(channel);
    }

    /**
     * fetch data by rule id
     *
     * @description  频道全部列表
     * @param
     * @return
     */
    public List<Channel> findAll(){
        return channelDao.findAll();
    }

    /**
     * fetch data by rule id
     *
     * @description  根据ID查询频道
     * @param
     * @return
     */
    public Channel findById(String id){
        return channelDao.findById(id).get();
    }

    /**
     * fetch data by rule id
     *
     * @description  修改频道
     * @param
     * @return
     */
    public void update(Channel channel){
        channelDao.save(channel);
    }

     /**
     * fetch data by rule id
     *
     * @description  根据ID删除频道
     * @param
     * @return
     */
     public void delete(String id){
         channelDao.deleteById(id);
     }

     /**
      * fetch data by rule id
      *
      * @description  构造查询条件
      * @param
      * @return
      */
     public Specification<Channel> createSpecification(Map searchMap){
         return new Specification<Channel>(){
             @Override
             public Predicate toPredicate(Root<Channel> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                 List<Predicate> predicateList = new ArrayList<>();
                 if(searchMap.get("name")!=null && !"".equals(searchMap.get("name"))){
                     predicateList.add(cb.like(root.get("name").as(String.class),"%"+(String) searchMap.get("name")+"%"));
                 }
                 if(searchMap.get("state")!=null && !"".equals(searchMap.get("state"))){
                     predicateList.add(cb.equal(root.get("state").as(String.class),(String) searchMap.get("state")));
                 }
                 return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));
             }
         };
     }

    /**
     * fetch data by rule id
     *
     * @description  根据条件查询频道列表
     * @param
     * @return
     */
    public List<Channel> findSearch(Map searchMap){
        Specification<Channel> specification = createSpecification(searchMap);
        return channelDao.findAll(specification);
    }


    /**
     * fetch data by rule id
     *
     * @description  频道分页
     * @param
     * @return
     */
    public Page<Channel> findSearch(Map searchMap,int page,int size){
        Specification<Channel> specification = createSpecification(searchMap);
        PageRequest pageRequest = PageRequest.of(page-1, size);
        return channelDao.findAll(specification,pageRequest);
    }

}
